//
//  HeroVideoDetailTableViewController.m
//  LOLProject
//
//  Created by lanou3g on 15/6/2.
//  Copyright (c) 2015年 lanou3g.com蓝欧科技. All rights reserved.
//

#import "HeroVideoDetailTableViewController.h"

@interface HeroVideoDetailTableViewController ()

@end

@implementation HeroVideoDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
//    设置返回UIBarButtonItem
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goBackToHeroVideo)];
//    注册所需要的cell
    [self.tableView registerNib:[UINib nibWithNibName:@"CommonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kHeroVideoDetailTableViewCell];
//    从网上请求数据
    [self requestHeroVideoDetailData];
}

#pragma mark---实现给返回UIBarButtonItem添加的方法
- (void) goBackToHeroVideo{
    [self dismissViewControllerAnimated:YES completion:nil];
    return ;
}

#pragma mark---懒加载英雄视频详情数组
- (NSMutableArray *) heroVideoDetaildataArray{
    if (_heroVideoDetaildataArray==nil) {
        self.heroVideoDetaildataArray=[NSMutableArray array];
    }
    return _heroVideoDetaildataArray;
}

#pragma mark---从网上请求数据
- (void) requestHeroVideoDetailData{
//    异步连接下的GET请求
//    1.创建网址对象
    NSString * string=[[NSString stringWithFormat:kHeroVideoDetailData_List_URL,[self.hero.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",self.heroVideoDetaildataArray.count/20+1]];
    NSURL * url=[NSURL URLWithString:string];
//    2.创建网络请求对象
    NSURLRequest * request=[NSURLRequest requestWithURL:url];
//    3.创建连接对象,并发送请求
    __weak HeroVideoDetailTableViewController * heroVideoDetailTVC=self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        代码保护
        if (data==nil) {
            return ;
        }
//        4.解析数据
        NSDictionary * sourceDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        处理数据(封装model对象)
        for (NSDictionary * dic in [sourceDic objectForKey:@"videos"]) {
            TheLatestVideoList * heroVideoDetailList=[[TheLatestVideoList alloc] init];
            [heroVideoDetailList setValuesForKeysWithDictionary:dic];
            [heroVideoDetailTVC.heroVideoDetaildataArray addObject:heroVideoDetailList];
        }
//        更显数据
        [heroVideoDetailTVC.tableView reloadData];
    }];
    return ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//设置有多少区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

//设置某区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [self.heroVideoDetaildataArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    回收cell
     CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHeroVideoDetailTableViewCell forIndexPath:indexPath];
//    回收不成功,就创建一个cell
    if (!cell) {
        cell=[[CommonTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kHeroVideoDetailTableViewCell];
    }
//    找到对应的heroVideoDetailList对象
    TheLatestVideoList * heroVideoDetailList=[self.heroVideoDetaildataArray objectAtIndex:indexPath.row];
//    传值
    cell.theLatestVideoList=heroVideoDetailList;
//    将信息显示在cell上
    [cell showTheLatestVideoListOnCommonTableViewCell];
    return cell;
}

#pragma mark---设置某行的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

#pragma mark---点击cell时触发的方法
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    找到对应的对象
    TheLatestVideoList * heroVideoDetailList=[self.heroVideoDetaildataArray objectAtIndex:indexPath.row];
    WebViewController * webVC=[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]];
    UINavigationController * webNC=[[UINavigationController alloc] initWithRootViewController:webVC];
    webVC.navigationItem.title=heroVideoDetailList.title;
    webVC.Url=heroVideoDetailList.link;
    [self presentViewController:webNC animated:YES completion:nil];
    return ;
}

#pragma mark---加载更多的英雄视频
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==[self.heroVideoDetaildataArray count]-1) {
//            从网上请求数据
        [self requestHeroVideoDetailData];
    }
    return ;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
